package com.example.xiongmao.service.impl;

import com.example.xiongmao.constant.MessageConstant;
import com.example.xiongmao.constant.StatusConstant;
import com.example.xiongmao.entity.DTO.CommonStatus;
import com.example.xiongmao.entity.DTO.admin.addAdminDTO;
import com.example.xiongmao.entity.DTO.admin.adminChargeDTO;
import com.example.xiongmao.entity.LoginDTO;
import com.example.xiongmao.entity.PageResult;
import com.example.xiongmao.entity.VO.admin.adminChargeVO;
import com.example.xiongmao.entity.Admin;
import com.example.xiongmao.exception.AccountLockedException;
import com.example.xiongmao.exception.AccountNotFoundException;
import com.example.xiongmao.exception.PasswordErrorException;
import com.example.xiongmao.mapper.adminChargeMapper;
import com.example.xiongmao.service.adminChargeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class adminChargeServiceImpl implements adminChargeService {
    @Autowired
    adminChargeMapper adminchargeMapper;

    @Override
    public PageResult list(adminChargeDTO adminchargeDTO) {
        // 设置分页参数
        PageHelper.startPage(adminchargeDTO.getPage(), adminchargeDTO.getPageSize());

        // 调用mapper方法进行查询
        List<adminChargeVO> pageResults = adminchargeMapper.list(adminchargeDTO);
        Page page = (Page) pageResults;
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增admin
     * @param addAdmin
     */
    @Override
    public void add(addAdminDTO addAdmin) {
        adminchargeMapper.add(addAdmin);
    }

    /**
     * 编辑回显
     *
     * @param id
     * @return
     */
    @Override
    public adminChargeVO getbyId(Integer id) {
        adminChargeVO admins = adminchargeMapper.getById(id);
        return admins;
    }

    /**
     * 编辑员工信息
     *
     * @param addAdmin
     */
    @Override
    public void updateAdmin(addAdminDTO addAdmin) {
        adminchargeMapper.updateAdmin(addAdmin);
    }

    /**
     * （批量）删除用户信息
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        adminchargeMapper.deleteAdminBatch(ids);
    }

    /**
     * 修改角色状态
     *
     * @param adminStatus
     */
    @Override
    public void adminStatusChange(CommonStatus adminStatus) {
        System.out.println("当前用户状态:" + adminStatus.getStatus());
        if ("1".equals(adminStatus.getStatus())) {
            // status==1表示正常，需要禁用==0
            adminchargeMapper.adminDisable(adminStatus.getId());
        } else {
            // status==0表示禁用，需要正常==1
            adminchargeMapper.adminunDisable(adminStatus.getId());
        }
    }

    // 登录
    @Override
    public Admin login(LoginDTO adminLogin) {
        String username = adminLogin.getUsername();
        String password = adminLogin.getPassword();

        Admin admin = adminchargeMapper.getByUserName(username);
        if (admin == null) {
            // 账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //需要进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println("当前被加密的密码：" + password);

        if (!password.equals(admin.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        if (admin.getStatus() == StatusConstant.DISABLE) {
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        return admin;
    }

    /**
     * admin注册
     * @param adminregistDTO
     */
    @Override
    public void addRegister(Admin adminregistDTO) {
        String password = adminregistDTO.getPassword();
        // 给密码加密存入数据库
        adminregistDTO.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        adminchargeMapper.addRegister(adminregistDTO);
    }
}
